package java8.lambda.demo04lambda;

import com.google.common.base.Function;
import java8.lambda.demo01.AbstractAppleBeforeTest;
import java8.lambda.demo01.Apple;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.function.BiFunction;
import java.util.function.Consumer;

import static java.util.Comparator.comparing;

/**
 * @author ChangLiang
 * @date 2020/4/22
 */
public class MethodReferencesTest extends AbstractAppleBeforeTest {

    @Test
    @DisplayName("test method references")
    public void test(){
        doConsume(System.out::println,"aaa");
    }

    @Test
    @DisplayName("test method references")
    public void test2(){
        Function<String, Integer> function = Integer::parseInt;
        Integer num = function.apply("123");
    }

    @Test
    @DisplayName("using method references to do sort")
    public void test3() {
        list.sort(comparing(Apple::getColor));
        System.out.println(list);
    }

    @Test
    @DisplayName("test BiFunction")
    public void test4() {
        BiFunction<String, Integer, Character> function = String::charAt;
        Character character = function.apply("hello", 2);
        System.out.println(character);
    }

    private static <T> void doConsume(Consumer<T> consumer, T t) {
        consumer.accept(t);
        consumer.accept(t);
    }
}
